package cn.edu.neusoft.yuejian12.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.yuejian12.Model.TrainModel;
import cn.edu.neusoft.yuejian12.R;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 木子李 on 2017/10/18.
 */

public class OrderActivity extends AppCompatActivity {
    private Button train_buy;
    private ImageView train_back;
    private EditText participantname,participanttel,adultnum;
    private TextView pay,trainHname,train_price,paynotice;
    private String train_id;
    private String str_name;
    private String bmnotice;
    private String str_train_name;
    private String str_tel;
    private String train_name;
    private String str_buynum;
    private String str_paybotice,str_pay;
    private Double price,allprice;
    private int user_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_train_order);
        initViews();
        initEvents();
    }
    private void initViews(){
        trainHname = (TextView) findViewById(R.id.train_name);
        participantname = (EditText) findViewById(R.id.participantname);
        participanttel = (EditText) findViewById(R.id.participanttel);
        adultnum = (EditText) findViewById(R.id.adultnum);
        paynotice = (TextView) findViewById(R.id.paynotice);
        train_buy = (Button) findViewById(R.id.train_buy);
        train_back = (ImageView) findViewById(R.id.train_back);
        pay = (TextView) findViewById(R.id.pay);
        Intent intent = getIntent();
        train_id = intent.getStringExtra("train_id");
        train_name = intent.getStringExtra("name");
        bmnotice = intent.getStringExtra("bmnotice");
        price = Double.valueOf(intent.getStringExtra("price"));
        user_id = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id",0);
        str_buynum = adultnum.getText().toString();
        Double buynum = Double.parseDouble(str_buynum);
        allprice = buynum*price;
        pay.setText(allprice.toString());
        trainHname.setText(train_name.toString());
        paynotice.setText(bmnotice.toString());


    }
    private void initEvents(){
        train_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        train_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buy();
            }
        });
    }
    private void buy(){

        str_name = participantname.getText().toString();
        str_tel = participanttel.getText().toString();
        str_paybotice = paynotice.getText().toString();
        str_buynum = adultnum.getText().toString();
        str_pay=allprice.toString();
        TrainModel trainModel = new TrainModel();
        Call<Integer> call = trainModel.applyTrain(str_name,train_id,str_tel,str_buynum,"1",str_pay,str_paybotice,user_id);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()!=0){
                    Toast.makeText(OrderActivity.this,"下单成功",Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(OrderActivity.this,"下单失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                Toast.makeText(OrderActivity.this,"获取连接失败",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

